/*
 * DataSourcePanel.java
 * Created on May 20, 2008, 3:05 AM
 */

package org.opennpo.mpres.gui;

import java.awt.Component;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Vector;
import javax.swing.Icon;
import org.opennpo.mpres.ListenerList;
import org.opennpo.mpres.ScriptItem;
import org.opennpo.mpres.ScriptItemListener;
import org.opennpo.mpres.ScriptItemSource;

/**
 * Control that displays the available ScriptItemSource's in a tab view.  
 * It uses java.util.ServiceLoader to discover those services that provide 
 * ScriptItemSource.  So any class that is registered to provide that interface 
 * will automatically get added to this panel.
 * @author  Nate Jones
 * @see java.util.ServiceLoader
 * @see org.opennpo.mpres.ScriptItemSource
 */
public class DataSourcePanel extends javax.swing.JPanel implements ScriptItemSource, ScriptItemListener{
    private List<ScriptItemSource> sources;
    private ListenerList listeners;
    
    /** 
     * Creates new form DataSourcePanel 
     */
    public DataSourcePanel() {
        listeners = new ListenerList();
        initComponents();
        sources = new Vector<ScriptItemSource>();
        ServiceLoader<ScriptItemSource> loader = ServiceLoader.load(ScriptItemSource.class);
        if(loader!=null){
            for(ScriptItemSource src : loader){
                addSource(src);
            }
        }
    }
    
    /**
     * Adds a new ScriptItemSource to the user interface.
     * @param src
     */
    public void addSource(ScriptItemSource src){
        tabPane.addTab(src.getTitle(), src.getIcon(), src.getComponent());
        sources.add(src);
        src.addListener(this);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabPane = new javax.swing.JTabbedPane();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));
        add(tabPane);
    }// </editor-fold>//GEN-END:initComponents
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane tabPane;
    // End of variables declaration//GEN-END:variables

    @Override
    public Icon getIcon() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getTitle() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Component getComponent() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void addListener(ScriptItemListener l) {
        listeners.add(l);
    }

    @Override
    public void removeListener(ScriptItemListener l) {
        listeners.remove(l);
    }

    @Override
    public void next() {
        
    }

    @Override
    public void itemSelected(ScriptItemSource src, ScriptItem item) {
        listeners.fireItemSelected(src, item);
    }
}
